import GameWS from "../../Managers/GameWS";
import LanguageMngTs from "../../Managers/LanguageMngTs";
import GameLogicUtilTs from "../../Utils/GameLogicUtilTs";

cc.Class({
    extends: cc.Component,

    properties: {
        winRootMenu:{
            default:null,
            type:cc.Node
        },

        winSound:{
            default:null,
            type:cc.Node
        },

        winLanguage:{
            default:null,
            type:cc.Node
        },

        winPrivacy:{
            default:null,
            type:cc.Node
        },

        winHowToPlay:{
            default:null,
            type:cc.Node
        },

        bgButton:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.bgButton.active = false
        this.winRootMenu.active = false
        this.winSound.active = false
        this.winLanguage.active = false
    },

    start () {

    },

    // update (dt) {},

    onLobbyMenu(event){
        let btnName = event.target.name
        if(btnName === "btn_howtoplay"){
            cc.gg.tdUtil.doSendTdEvent("click_menu_howtoplay","Menu")
            this.bgButton.active = true
            this.winLanguage.height = cc.winSize.height
            this.windowEnterFromRight(this.winHowToPlay)
            this.hideRootMenu()
        }else if(btnName === "btn_sound"){
            cc.gg.tdUtil.doSendTdEvent("click_menu_setting","Menu")
            this.bgButton.active = true
            this.winSound.height = cc.winSize.height
            this.windowEnterFromRight(this.winSound)
            this.hideRootMenu()
        }else if(btnName === "btn_language"){
            cc.gg.tdUtil.doSendTdEvent("click_menu_language","Menu")
            this.bgButton.active = true
            this.winLanguage.height = cc.winSize.height
            this.windowEnterFromRight(this.winLanguage)
            this.hideRootMenu()
        }else if(btnName === "btn_privicy"){
            cc.gg.tdUtil.doSendTdEvent("click_menu_privicy","Menu")
            this.bgButton.active = true
            this.winPrivacy.height = cc.winSize.height
            this.windowEnterFromRight(this.winPrivacy)
            this.hideRootMenu()
        }else if(btnName === "btn_logout"){
            this._askLogout()
        }else if(btnName === "BgButton"){
            this.bgButton.active = false
            if(this.winRootMenu.active){
                this.windowOutToRight(this.winRootMenu)
            }else if(this.winLanguage.active){
                this.windowOutToRight(this.winLanguage)
            }else if(this.winSound.active){
                this.windowOutToRight(this.winSound)
            }else if(this.winHowToPlay.active){
                this.windowOutToRight(this.winHowToPlay)
            }
        }
    },

    _askLogout(){
        cc.gg.tdUtil.doSendTdEvent("click_menu_logout","Menu")

        let title = LanguageMngTs.getSentence(73)//"Logout"
        let positiveStr = LanguageMngTs.getSentence(57)//"Yes"
        let passiveStr = LanguageMngTs.getSentence(179)//"Cancel"
        cc.gg.toastMgr.showDialog(title,positiveStr,passiveStr,LanguageMngTs.getSentence(300),//Do you want to logout?
            async function () {
                cc.gg.toastMgr.removeDialog()
                cc.gg.tdUtil.doSendTdEvent("click_menu_logout_yes","Menu")

                cc.gg.toastMgr.showNetWaitting()
                let result = await cc.gg.httpUtil.httpGet("/user/logout")
                await GameWS.closeWs()
                GameLogicUtilTs.forceLogoutWs = true
                cc.gg.toastMgr.removeNetWaitting()

                if(!result.succ){
                    cc.gg.toastMgr.showToast("Logout failed,please try again.",2)
                    return
                }

                cc.gg.gameDataUtil.shownDailyBonus = false
                cc.sys.localStorage.removeItem('user_token');
                cc.gg.httpUtil.setUserToken(null)
                cc.gg.gameDataUtil.goToLogin(true)
            }.bind(this),
            function () {
                cc.gg.tdUtil.doSendTdEvent("click_menu_logout_cancel","Menu")
                cc.gg.toastMgr.removeDialog()
            }
        )
    },

    onLobbyMenuClosed:function (event) {
        if(this.bgButton.active) this.bgButton.active = false
        this.windowOutToRight(event.target.parent)
    },

    onSecondaryMenuClosed:function (event) {
        if(this.bgButton.active) this.bgButton.active = false
        this.windowOutToRight(event.target.parent)
        this.showRootMenu()
    },

    showRootMenu:function () {
        this.bgButton.active = true
        this.winRootMenu.height = cc.winSize.height
        this.windowEnterFromRight(this.winRootMenu)
    },

    hideRootMenu:function () {
        this.windowOutToRight(this.winRootMenu)
    },


    windowEnterFromRight:function (node) {
        node.active = true
        node.x = cc.winSize.width/2 + node.width/2
        node.y = 0
        node.runAction(cc.moveBy(0.12,-node.width,0))
    },

    windowOutToRight:function (node) {
        node.active = true
        node.y = 0
        let seq = cc.sequence(cc.moveTo(0.12,cc.winSize.width/2+node.width/2,0),
            cc.callFunc(function () {
                this.active = false
            }.bind(node),this))
        node.runAction(seq)
    },
});
